/**
 * Created by liyun on 2015/10/28.
 */
var mqtt = require('mqtt');
var mqttclient = mqtt.connect('mqtt://115.28.90.45:1883');
mqttclient.subscribe('fault');
mqttclient.on('message', function (topic, message) {
    console.log(message.split(' ')[0] + "----------" + message.split(' ')[1]);
    if (message.split(' ')[1] == 1) {
        new Fault({
            sim: message.split(' ')[0],
            date: new Date(),
            ftype: message.split(' ')[1], //操作内容
            content: "连不上互联网"
        }).save();
    } else if (message.split(' ')[1] == 2) {
        new Fault({
            sim: message.split(' ')[0],
            date: new Date(),
            ftype: message.split(' ')[1], //操作内容
            content: "连不上服务器"
        }).save();
    } else if (message.split(' ')[1] == 3) {
        new Fault({
            sim: message.split(' ')[0],
            date: new Date(),
            ftype: message.split(' ')[1], //操作内容
            content: "Sim卡欠费"
        }).save();
    } else if (message.split(' ')[1] == 4) {
        new Fault({
            sim: message.split(' ')[0],
            date: new Date(),
            ftype: message.split(' ')[1], //操作内容
            content: "连接不上Sim卡"
        }).save();
    }
});

module.exports = mqttclient;
